<template>
  <div class="layout layout-100">
    <Layout class="layout-100">
      <Header class="header-cont">人员详情</Header>
      <Content class="content-cont">
        <Tabs value="1" v-model="active" @on-click="activeTab" >
          <TabPane label="基本信息" name="1"></TabPane>
          <TabPane label="管理范围" name="2"></TabPane>
          <TabPane label="角色权限" name="3"></TabPane>
          <TabPane label="其他身份" name="4"></TabPane>
        </Tabs>
        <Form
          ref="formData"
          :model="formData"
          label-position="top"
          :rules="ruleValidate"
        >
          <Layout class="form-input" v-show="active ==1">
            <Layout  class="form-input">
              <Content class="content-cont">
                <Row :gutter="16">
                  <Col span="24">
                  <FormItem label="人员编号" prop="number">
                    <Input
                      style="width:350px;margin-right:40px;"
                      v-model.trim="formData.basicInfo.number"
                      :disabled="$route.params.type === 'details'"
                    />
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="姓名" prop="name">
                    <Input
                      v-model.trim="formData.basicInfo.name"
                      :disabled="$route.params.type === 'details'"
                      style="width:350px;"
                    />
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="身份证号" prop="idcard">
                    <Input
                      v-model.trim="formData.basicInfo.idcard"
                      :disabled="$route.params.type !== 'add'"
                      style="width:350px;"
                    />
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="联系电话">
                    <Input
                      v-model.trim="formData.basicInfo.tel"
                      :disabled="$route.params.type !== 'add'"
                      style="width:350px;"
                    />
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="职务">
                    <Select v-model="formData.basicInfo.postList" disabled multiple style="width:350px;" placeholder="请选择职务">
                      <OptionGroup label="通用">
                        <Option v-for="item in postList.generalPostList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                      </OptionGroup>
                      <OptionGroup label="专业">
                        <Option v-for="item in postList.proprietaryPostList" :value="item.id" :key="item.id">{{ item.name }}</Option>
                      </OptionGroup>
                    </Select>
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="工种">
                    <Select v-model="formData.basicInfo.dictWorkType" disabled style="width:350px;" placeholder="请选择工种">
                      <Option v-for="item in professionList" :value="item.code" :key="item.code">{{ item.name }}</Option>
                    </Select>
                  </FormItem>
                  </Col>

                  <Col span="12">
                  <FormItem label="入场日期">
                    <DatePicker v-model="formData.basicInfo.entryTime" disabled type="date" placeholder="请选择入场日期" style="width: 350px"></DatePicker>
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="人员状态">
                    <Select v-model="formData.basicInfo.checkStatus" disabled style="width:350px;" placeholder="请选择人员状态">
                      <Option v-for="item in personStatusList" :value="item.code" :key="item.code">{{ item.name }}</Option>
                    </Select>
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="应急联系人">
                    <Input
                      style="width:350px;"
                      v-model.trim="formData.basicInfo.emergencyContact"
                      :disabled="$route.params.type !== 'add'"
                    />
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="联系人手机">
                    <Input
                      style="width:350px;"
                      v-model.trim="formData.basicInfo.emergencyContactTel"
                      :disabled="$route.params.type !== 'add'"
                    />
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="政治面貌">
                    <Select v-model="formData.basicInfo.dictPoliticalStatus" disabled style="width:350px;" placeholder="请选择人员状态">
                      <Option v-for="item in dictPolitical" :value="item.code" :key="item.code">{{ item.name }}</Option>
                    </Select>
                  </FormItem>
                  </Col>
                  <Col span="12">
                  <FormItem label="宗教信仰">
                    <Select v-model="formData.basicInfo.dictReligion" disabled style="width:350px;" placeholder="请选择人员状态">
                      <Option v-for="item in dictReligion" :value="item.code" :key="item.code">{{ item.name }}</Option>
                    </Select>
                  </FormItem>
                  </Col>
                </Row>
                <Row>
                  <Col span="4">
                  <div>
                    <div
                      class="demo-upload-list"
                      v-for="item in uploadList"
                      :key="item.id"
                    >
                      <template v-if="item.status === 'finished'">
                        <!--<img :src="'https://user.qhse.cn/etcsso/images' + item.filePath ">-->
                        <img
                          :src="
                              fileUploadUrl +
                                '/file/downFile/' +
                                item.response.fileIds
                            "
                          style="width:150px;height:150px;"
                        />
                        <div class="demo-upload-list-cover">
                          <Icon
                            type="ios-eye-outline"
                            @click.native="handleView(1)"
                          ></Icon>
                        </div>
                      </template>
                      <template v-else>
                        <Progress
                          v-if="item.showProgress"
                          :percent="item.percentage"
                          hide-info
                        ></Progress>
                      </template>
                    </div>
                    <Upload
                      ref="upload"
                      :show-upload-list="false"
                      :default-file-list="defaultList"
                      :format="['jpg', 'jpeg', 'png']"
                      :max-size="2048"
                      :action="uploadAction"
                      type="drag"
                      style="display: inline-block; width:150px;"
                      v-show="
                          uploadList.length == 0 &&
                            $route.params.type != 'details'
                        "
                    >
                      <div class="pic1">
                        <Icon
                          type="ios-cloud-upload"
                          size="52"
                          style="color: #3399ff"
                        ></Icon>
                        <p>点击添加照片</p>
                      </div>
                      <Modal title="查看图片" width="50" v-model="visible">
                        <img
                          :src="
                              fileUploadUrl +
                                '/file/downFile/' +
                                uploadList[0].response.fileIds
                            "
                          v-if="visible"
                          style="width: 100%"
                        />
                      </Modal>
                    </Upload>
                  </div>
                  </Col>
                  <Col span="6">
                  <div>
                    <div
                      class="demo-upload-list1"
                      v-for="item in uploadList1"
                      :key="item.id"
                    >
                      <template v-if="item.status === 'finished'">
                        <!--<img :src="'https://user.qhse.cn/etcsso/images' + item.filePath ">-->
                        <img
                          :src="
                              fileUploadUrl +
                                '/file/downFile/' +
                                item.response.fileIds
                            "
                          style="width:250px;height:150px;"
                        />
                        <div class="demo-upload-list-cover1">
                          <Icon
                            type="ios-eye-outline"
                            @click.native="handleView(2)"
                          ></Icon>
                        </div>
                      </template>
                      <template v-else>
                        <Progress
                          v-if="item.showProgress"
                          :percent="item.percentage"
                          hide-info
                        ></Progress>
                      </template>
                    </div>
                    <Upload
                      ref="upload1"
                      :show-upload-list="false"
                      :default-file-list="defaultList"
                      :format="['jpg', 'jpeg', 'png']"
                      :max-size="2048"
                      :action="uploadAction"
                      type="drag"
                      style="display: inline-block; width:250px;"
                      v-show="
                          uploadList1.length == 0 &&
                            $route.params.type != 'details'
                        "
                    >
                      <div class="pic2">
                        <Icon
                          type="ios-cloud-upload"
                          size="52"
                          style="color: #3399ff"
                        ></Icon>
                        <p>身份证正面</p>
                      </div>
                      <Modal title="查看图片" width="50" v-model="visible1">
                        <img
                          :src="
                              fileUploadUrl +
                                '/file/downFile/' +
                                uploadList1[0].response.fileIds
                            "
                          v-if="visible1"
                          style="width: 100%"
                        />
                      </Modal>
                    </Upload>
                  </div>
                  </Col>
                  <Col span="14">
                  <div>
                    <div
                      class="demo-upload-list1"
                      v-for="item in uploadList2"
                      :key="item.id"
                    >
                      <template v-if="item.status === 'finished'">
                        <!--<img :src="'https://user.qhse.cn/etcsso/images' + item.filePath ">-->
                        <img
                          :src="
                              fileUploadUrl +
                                '/file/downFile/' +
                                item.response.fileIds
                            "
                          style="width:250px;height:150px;"
                        />
                        <div class="demo-upload-list-cover1">
                          <Icon
                            type="ios-eye-outline"
                            @click.native="handleView(3)"
                          ></Icon>
                        </div>
                      </template>
                      <template v-else>
                        <Progress
                          v-if="item.showProgress"
                          :percent="item.percentage"
                          hide-info
                        ></Progress>
                      </template>
                    </div>
                    <Upload
                      ref="upload2"
                      :show-upload-list="false"
                      :default-file-list="defaultList"
                      :format="['jpg', 'jpeg', 'png']"
                      :max-size="2048"
                      :action="uploadAction"
                      type="drag"
                      style="display: inline-block; width:250px;"
                      v-show="
                          uploadList2.length == 0 &&
                            $route.params.type != 'details'
                        "
                    >
                      <div class="pic2">
                        <Icon
                          type="ios-cloud-upload"
                          size="52"
                          style="color: #3399ff"
                        ></Icon>
                        <p>身份证背面</p>
                      </div>
                      <Modal title="查看图片" width="50" v-model="visible2">
                        <img
                          :src="
                              fileUploadUrl +
                                '/file/downFile/' +
                                uploadList2[0].response.fileIds
                            "
                          v-if="visible2"
                          style="width: 100%"
                        />
                      </Modal>
                    </Upload>
                  </div>
                  </Col>
                </Row>
              </Content>
            </Layout>
          </Layout>
          <div v-show="active == 2">
            <Row>
              <Col span="24">
              <div>
                <Layout>
                  <Header class="header-cont">主管部门</Header>
                  <Content class="content-cont">
                    <CheckboxGroup v-model="mangeDeptIdList">
                      <Checkbox v-for="item in mangeDeptList" :label="item.id" border :disabled="$route.params.type === 'details'">{{item.name}}</Checkbox>
                    </CheckboxGroup>
                  </Content>
                </Layout>
              </div>
              </Col>
              <Col span="24">
              <div>
                <Layout>
                  <Header class="header-cont">关注项目</Header>
                  <Content class="content-cont">
                    <div class="table-title">
                      <Icon style="float:right;margin-top:8px;" v-show="$route.params.type != 'details'" type="ios-add-circle-outline" size="24" color="#2d8cf0" />
                    </div>
                    <Table border :columns="columns" :data="formData.mangeScope.followProjectList">
                    </Table>
                  </Content>
                </Layout>
              </div>
              </Col>
            </Row>
          </div>
          <div v-show="active == 3">
            <Row>
              <Col span="24">
              <Checkbox v-model="formData.rolePerm.autoGenUser" disabled>自动生成用户</Checkbox>
              </Col>
              <Col span="24">
              <div>
                <Layout>
                  <Header class="header-cont" style="background: #F5F8FC">通用角色</Header>
                  <Content class="content-cont">
                    <CheckboxGroup @on-change="changeNormalRoleList" v-model="normalRoleIdList">
                      <Checkbox v-for="item in normalRoleList" disabled :label="item.id" border>{{item.name}}</Checkbox>
                    </CheckboxGroup>
                  </Content>
                </Layout>
              </div>
              </Col>
              <Col span="24">
              <div>
                <Layout>
                  <Header class="header-cont" style="background: #F5F8FC">专用角色</Header>
                  <Content class="content-cont">
                    <CheckboxGroup v-model="specialRoleIdList"  @on-change="changeSpecialRoleList">
                      <Checkbox v-for="item in specialRoleList" disabled :label="item.id" border>{{item.name}}</Checkbox>
                    </CheckboxGroup>
                  </Content>
                </Layout>
              </div>
              </Col>
            </Row>
          </div>
          <div v-show="active == 4">
            <template>
              <div>
                <Table border :columns="columns1" :data="formData.otherIdentity">
                  <template slot-scope="{ row, index }" slot="action">
                    <Icon type="ios-remove-circle-outline" @click="remove(index)" size="24" color="#2d8cf0" />
                  </template>
                </Table>
              </div>
            </template>
          </div>
        </Form>
      </Content>
    </Layout>
  </div>
</template>
<script>
  import { mapMutations } from 'vuex'
  import Cookies from 'js-cookie'
  import Layout from "view-design/src/components/layout/layout";
  export default {
    components: {Layout},
    name: 'ProjectPersonDetail',
    data() {
      return {

        typeList: [],
        formData: {
          rolePerm:{
            specialRoleList:[],
            normalRoleList:[],
            autoGenUser:false,
          },
          qualifications:[
          ],
          basicInfo:{
            number:'',
            entryTime:'',
            idcard:'',
            name:'',
            tel:'',
            postList:[],
            dictWorkType:'',
            checkStatus:''
          },
          mangeScope:{
            followProjectList:[],
            mangeDeptList:[]
          },
          otherIdentity:[]
        },
        id: this.$route.params.id,
        specialRoleIdList:[],//选择专业类型ID数组
        normalRoleIdList:[],//选择通用类型ID数组
        active:'1',
        columns: [
          {
            title: '序号',
            align: 'center',
            render: (h, params) => {
              return h('span', params.index + 1)
            }
          },
          {
            title: '项目名称',
            key: 'name',
            align: 'center',
          },
          {
            title: '工程类型',
            key: 'dictEngineeringTypeName',
            align: 'center',

          },
        ],
        columns1:[
          {
            title: '序号',
            align: 'center',
            width: 50,
            render: (h, params) => {
              return h('span', params.index + 1)
            }
          },
          {
            title: '所属机构',
            key: 'enterpriseName',
            align: 'center',
          },
          {
            title: '职务',
            key: 'postNames',
            align: 'center',

          },
          {
            title: '角色',

            key: 'roleNames',
            align: 'center'
          }
        ],
        visible:false,
        visible1:false,
        visible2:false,
        specialRoleList:[],//专用角色
        normalRoleList:[],//通用角色
        uploadAction:process.env.VUE_APP_FILE_SERVER_URL+'file/uploadFile',
        fileUploadUrl:process.env.VUE_APP_FILE_SERVER_URL,
        defaultList: [],//上传图片
        professionList:[],//工种
        postList:[],//职务类型
        personStatusList:[],//人员状态
        dictPolitical:[],//政治面貌
        dictReligion:[],//宗教信仰
        uploadList:[],
        uploadList1:[],
        uploadList2:[],
        followProjectList:[],
        mangeDeptList:[],
        mangeDeptIdList:[],
        ruleValidate: {
          number: [
            { required: true, message: '人员编号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '人员名称不能为空', trigger: 'blur' }
          ],
          idcard: [{ required: true, message: '身份证不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      ...mapMutations(['closeTag']),
      closetab() {
        /**
         * 如果是调用closeTag方法，普通的页面传入的对象参数只需要写name字段即可
         * 如果是动态路由和带参路由，需要传入query或params字段，用来区别关闭的是参数为多少的页面
         */
        this.closeTag({
          name: 'add-editor-details',
          params: {
            id: this.$route.params.id,
            name: this.$route.params.name,
            type: this.$route.params.type,
            deptId:this.$route.params.deptId
          }
        })
      },
      checkPersonBtn(name) {
        Cookies.remove('joinUser',{})
        let type = 1
        this.$router.push(`${name}/${type}`);
      },
      activeTab(code){
        this.active = code;
      },

      //获取职务类型
      getPostList(){
        this.$api.ProjectPerson.getJobList({}).then(res => {
          if (res.code === 200) {
            this.postList = res.data
          } else {
            this.$Message.error(res.message)
          }
        })
      },
      //详情
      getDetail(id) {
        this.$api.ProjectPerson.getPersonDetail(id).then(res => {
          if (res.code === 200) {
            this.formData = res.data
            let postIdList = []
            this.formData.basicInfo.postList.forEach(item => {
              if (item.selected) {
                postIdList.push(item.id)
              }
            })
            this.formData.basicInfo.postList = postIdList
            this.postList.generalPostList = this.formData.basicInfo.generalPostList
            this.postList.proprietaryPostList = this.formData.basicInfo.proprietaryPostList
            if (this.formData.basicInfo.entryTime) {
              this.formData.basicInfo.entryTime = this.$time(
                new Date(this.formData.basicInfo.entryTime),
                'yyyy-MM-dd'
              )
            }
            if (this.formData.rolePerm.autoGenUser == 0) {
              this.formData.rolePerm.autoGenUser = false
            } else if (this.formData.rolePerm.autoGenUser == 1) {
              this.formData.rolePerm.autoGenUser = true
            }
            if (this.formData.basicInfo.avatar) {
              let upload = [
                {
                  status: 'finished',
                  showProgress: false,
                  response: {
                    fileIds: this.formData.basicInfo.avatar
                  }
                }
              ]
              this.uploadList = upload
            }
            if (this.formData.basicInfo.idFrontPhoto) {
              let upload1 = [
                {
                  status: 'finished',
                  showProgress: false,
                  response: {
                    fileIds: this.formData.basicInfo.idFrontPhoto
                  }
                }
              ]
              this.uploadList1 = upload1
            }
            if (this.formData.basicInfo.idReversePhoto) {
              let upload2 = [
                {
                  status: 'finished',
                  showProgress: false,
                  response: {
                    fileIds: this.formData.basicInfo.idReversePhoto
                  }
                }
              ]
              this.uploadList2 = upload2
            }
            this.specialRoleIdList = []
            this.normalRoleIdList = []
            this.specialRoleList = []
            this.normalRoleList = []
            this.mangeDeptList = []
            if(this.$route.params.type !== 'details'){
              this.specialRoleList = this.formData.rolePerm.specialRoleList
              this.normalRoleList = this.formData.rolePerm.normalRoleList
              this.mangeDeptList = this.formData.mangeScope.mangeDeptList
            }
            this.formData.rolePerm.specialRoleList.forEach(item => {
              if (item.selected == true) {
                if(this.$route.params.type === 'details'){
                  this.specialRoleList.push(item);
                }
                this.specialRoleIdList.push(item.id)
              }
            })
            this.formData.rolePerm.normalRoleList.forEach(item => {
              if (item.selected == true) {
                if(this.$route.params.type === 'details'){
                  this.normalRoleList.push(item);
                }
                this.normalRoleIdList.push(item.id)
              }
            })
            if (this.formData.qualifications.length > 0) {
              this.formData.qualifications.forEach(item => {
                item.expiredTime = this.$time(
                  new Date(item.expiredTime),
                  'yyyy-MM-dd'
                )
              })
            }
            this.formData.mangeScope.mangeDeptList.forEach(item => {
              if (item.selected == true) {
                if(this.$route.params.type === 'details'){
                  this.mangeDeptList.push(item);
                }
                this.mangeDeptIdList.push(item.id)
              }
            })
            Cookies.remove('joinUser', {})
          }
        })
      },
      //获取工种
      getProfessionList(){
        let code = 'PROFESSION'
        this.$api.ProjectPerson.getListByTypeCode(code).then(res => {
          if (res.code === 200) {
            this.professionList = res.data
          } else {
            this.$Message.error(res.message)
          }
        })
      },
      //获取证件类型
      getLicenseType(){
        let code = 'LICENSE_TYPE'
        this.$api.ProjectPerson.getListByTypeCode(code).then(res => {
          if (res.code === 200) {
            this.licenseTypeList = res.data
          } else {
            this.$Message.error(res.message)
          }
        })
      },
      //获取人员状态
      getPersonStatus(){
        let code = 'PERSON_STATE'
        this.$api.ProjectPerson.getListByTypeCode(code).then(res => {
          if (res.code === 200) {
            this.personStatusList = res.data
          } else {
            this.$Message.error(res.message)
          }
        })
      },
      //获取政治面貌
      getDictPolitical(){
        let code = 'POLITICS_STATUS'
        this.$api.ProjectPerson.getListByTypeCode(code).then(res => {
          if (res.code === 200) {
            this.dictPolitical = res.data
          } else {
            this.$Message.error(res.message)
          }
        })
      },
      //获取宗教信仰
      getDictReligion(){
        let code = 'PERSON_RELIGION'
        this.$api.ProjectPerson.getListByTypeCode(code).then(res => {
          if (res.code === 200) {
            this.dictReligion = res.data
          } else {
            this.$Message.error(res.message)
          }
        })
      },

      //选择通用角色
      changeNormalRoleList(){
        this.formData.rolePerm.normalRoleList = [];
        this.normalRoleList.forEach(item=>{
          this.normalRoleIdList.forEach(val=>{
            if(item.id == val){
              this.formData.rolePerm.normalRoleList.push(item);
            }
          })
        })
      },
      //选择专业角色
      changeSpecialRoleList(){
        this.formData.rolePerm.specialRoleList = [];
        this.specialRoleList.forEach(item=>{
          this.specialRoleIdList.forEach(val=>{
            if(item.id == val){
              this.formData.rolePerm.specialRoleList.push(item);
            }
          })
        })
      },



      /*上传图片开始.....................................*/

      handleView (code) {//查看
        if(code==1){
          this.visible = true;
        }else if(code == 2){
          this.visible1 = true;
        }else{
          this.visible2 = true;
        }


      },
      initParams(){
        this.page = this.$route.query;
      }
    },
    mounted() {
      this.getProfessionList();
      this.getPersonStatus();
      this.getDictPolitical();
      this.getDictReligion();
      this.getLicenseType();
      this.getDetail(this.$route.params.id)
      this.uploadList = this.$refs.upload.fileList;
      this.uploadList1 = this.$refs.upload1.fileList;
      this.uploadList2 = this.$refs.upload2.fileList;

    }
  }
</script>

<style lang="scss">
  .width50 {
    width: 70%;
  }
  .Img-class{
    background-color: transparent;
  }
  .form-input{
    padding:20px;
  }
  .pic1{
    width:150px;
    height:150px;
    border-radius:5px;
    padding-top:40%;
  }
  .pic2{
    width:250px;
    height:150px;
    border-radius:5px;
    line-height: 150px;
    margin-left:20px;
    padding-right:40px;
  }
  .table-title{
    height: 50px;
    line-height: 50px;
    border: 1px solid #dcdee2;
    border-bottom: none;
    padding: 0 10px;
  }
  .demo-upload-list{
    display: inline-block;
    width: 150px;
    height: 150px;
    text-align: center;
    line-height: 150px;
    border: 1px solid transparent;
    border-radius: 5px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .demo-upload-list-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .demo-upload-list:hover .demo-upload-list-cover{
    display: block;
  }
  .demo-upload-list-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
  .demo-upload-list1{
    display: inline-block;
    width: 250px;
    height: 150px;
    text-align: center;
    line-height: 150px;
    border: 1px solid transparent;
    border-radius: 5px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .demo-upload-list-cover1{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .demo-upload-list1:hover .demo-upload-list-cover1{
    display: block;
  }
  .demo-upload-list-cover1 i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
</style>
